

#include <iostream>

#include "../common/httplib.h"
#include "oj_control.hpp"

using namespace rs_control;
using namespace httplib;
static Control* ctrl_ptr = nullptr;

void Recovery(int signo)
{
    ctrl_ptr ->RecoverrAll();
}

int main()
{
    signal(SIGQUIT,Recovery);
    Server svr;
    rs_control::Control ctrl;
    ctrl_ptr = &ctrl;
    svr.Get("/all_questions",[&ctrl](const Request& req,Response &resp){
        std::string html;
        ctrl.AllQuestions(&html);
        resp.set_content(html,"text/html; charser=utf-8");
        
    });

    svr.Get(R"(/question/(\d+))",[&ctrl](const Request&req,Response &resp)
    {
        std::string num = req.matches[1];
        std::string html;
        ctrl.OneQuestion(num,&html);
        resp.set_content(html,"text/html; charser=utf-8");
    });
    
    svr.Post(R"(/judge/(\d+))",[&ctrl](const Request&req,Response& resp){
        std::string num = req.matches[1];
        std::string res_result;
        ctrl.Judge(num,req.body,&res_result);
        resp.set_content(res_result,"text/plain; charser=utf-8");
    });

    svr.set_base_dir("./wwwroot");

    svr.listen("0.0.0.0",8080);
    
    return 0;
}